package system;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Clock;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import DB.DB_connection_pool;

public class Opinion_feedback extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		Clock clock = Clock.systemUTC();
		String idea_feedback_id = String.valueOf((int)(Math.random()*900000)+100000)+clock.millis();  //反馈意见编号;
		java.sql.Date idea_feedback_date = new java.sql.Date(clock.millis());     //当前时间
		String user_account = request.getSession().getAttribute("user_account").toString();
		String idea_feedback_theme = request.getParameter("idea_feedback_theme");
		String idea_feedback_content = request.getParameter("idea_feedback_content");
		
		String insert_sql = "insert into idea_feedback(idea_feedback_id ,idea_feedback_date ,user_account ,idea_feedback_theme ,idea_feedback_content) values(?,?,?,?,?)";
		JSONObject jsonObj = new JSONObject();
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_insert = conn.prepareStatement(insert_sql);
			ps_insert.setString(1, idea_feedback_id);
			ps_insert.setDate(2, idea_feedback_date);
			ps_insert.setString(3, user_account);
			ps_insert.setString(4, idea_feedback_theme);
			ps_insert.setString(5, idea_feedback_content);
			
			if(ps_insert.executeUpdate()!=0){
				jsonObj.put("result", true);
			}else{
				jsonObj.put("result", false);
			}
			ps_insert.close();
			conn.close();
		} catch (SQLException e) {
			jsonObj.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jsonObj.toString());
	}

}
